package org.example.biz.config;

import com.anwen.mongo.handlers.MetaObjectHandler;
import java.time.LocalDateTime;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.stereotype.Component;

/**
 * @Title : UpdateableMetaObjectHandler
 * @Description: 更新类操作自动更新数据
 * @author: libo@juzishuke.com
 * @date: 2023/11/16 16:21
 * @Version:1.0
 */
@Slf4j
@Component
public class UpdateableMongoMetaObjectHandler implements MetaObjectHandler {
    // 插入式的填充策略
    @Override
    public void insertFill(Map<String, Object> map, Document document) {
        log.info("开始执行了插入操作-------------------------------------");
        map.keySet().forEach(key -> {
            if (key.equals("createTime")){
                document.put(key, LocalDateTime.now());
            }
            if (key.equals("updateTime")){
                document.put(key, LocalDateTime.now());
            }
        });
    }
    // 更新式的填充策略
    @Override
    public void updateFill(Map<String, Object> map, Document document) {
        log.info("开始执行了更新操作-------------------------------------");
        map.keySet().forEach(key -> {
            if (key.equals("updateTime")){
                document.put(key, LocalDateTime.now());
            }
        });
    }

}
